<?php defined('SYSPATH') OR die('No direct access allowed.');
class Auth extends Auth_Core
{
	/**
	 * openid user login
	 * @param int | User_Model 
	 * @param boolean remember login
	 */
	public function openid_login($user,$remember = true)
	{
		if(is_int($user))
		{
			$user = ORM::factory('user',$user);
		}
		if($remember)
		{
			// Create a new autologin token
			$token = ORM::factory('user_token');

			// Set token data
			$token->user_id = $user->id;
			$token->expires = time() + $this->config['lifetime'];
			$token->save();

			// Set the autologin cookie
			cookie::set('authautologin', $token->token, $this->config['lifetime']);
		}
		//copy from auth/drivers/ORM.php

		// Update the number of logins
		$user->logins += 1;
		// Set the last login date
		$user->last_login = time();
		$user->last_login_ip = Input::instance()->ip_address();
		// Save the user
		$user->save();
		// Regenerate session_id
		Session::instance()->regenerate();
		// Store username in session
		$_SESSION[$this->config['session_key']] = $user;
	}

	public function login($username, $password, $remember = FALSE)
	{
		if (empty($password))
			return FALSE;

		if (is_string($password))
		{
			// Get the salt from the stored password
			// should check if the password is empty
			if($real_password = $this->driver->password($username))
			{
				$salt = $this->find_salt($real_password);
			}
			else
			{
				return false;
			}

			// Create a hashed password using the salt from the stored password
			$password = $this->hash_password($password, $salt);
		}

		return $this->driver->login($username, $password, $remember);
	}
}
